﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SDT.CrawlSystem.Servers.Filters
{
    public static class LeadFilter
    {
        private const string BrChar = "&lt;br&gt;";
        private const string PBreakChar = "&lt;/p&gt;";
        private const string PChar = "&lt;p&gt;";

        public static List<string> ProcessLead(string Content)
        {
            List<string> ListResult = new List<string>();
            if (!string.IsNullOrEmpty(Content))
            {
                string ContentEncode = HttpUtility.HtmlEncode(Content);
                int indexofValueBr = ContentEncode.IndexOf(BrChar);
                int indexofValuePBreak = ContentEncode.IndexOf(PBreakChar);
                int indexofValueP = ContentEncode.IndexOf(PChar, 30);


                if (indexofValueBr > 0 && indexofValuePBreak > 0 && indexofValueP > 0)
                {
                    int[] rangeIndex = { indexofValueBr, indexofValuePBreak, indexofValueP };
                    ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Substring(0, rangeIndex.Min())));
                    ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Remove(0, rangeIndex.Min())));
                    goto ResultFunct;
                }

                if (indexofValueBr > 0)
                {
                    if (indexofValuePBreak == -1 && indexofValueP == -1)
                    {
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Substring(0, indexofValueBr)));
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Remove(0, indexofValueBr)));
                        goto ResultFunct;
                    }
                    else if (indexofValuePBreak > 0 && indexofValueP == -1)
                    {
                        int[] rangeIndex = { indexofValueBr, indexofValuePBreak };
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Substring(0, rangeIndex.Min())));
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Remove(0, rangeIndex.Min())));
                        goto ResultFunct;
                    }
                    else
                    {
                        int[] rangeIndex = { indexofValueBr, indexofValueP };
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Substring(0, rangeIndex.Min())));
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Remove(0, rangeIndex.Min())));
                        goto ResultFunct;
                    }

                }

                if (indexofValuePBreak > 0)
                {
                    if (indexofValueBr == -1 && indexofValueP == -1)
                    {
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Substring(0, indexofValuePBreak)));
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Remove(0, indexofValuePBreak)));
                        goto ResultFunct;
                    }
                    else if (indexofValueBr > 0 && indexofValueP == -1)
                    {
                        int[] rangeIndex = { indexofValuePBreak, indexofValueBr };
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Substring(0, rangeIndex.Min())));
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Remove(0, rangeIndex.Min())));
                        goto ResultFunct;
                    }
                    else
                    {
                        int[] rangeIndex = { indexofValuePBreak, indexofValueP };
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Substring(0, rangeIndex.Min())));
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Remove(0, rangeIndex.Min())));
                        goto ResultFunct;
                    }
                }
                if (indexofValueP > 0 && indexofValueBr == -1 && indexofValuePBreak == -1)
                {
                    if (indexofValueBr == -1 && indexofValuePBreak == -1)
                    {
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Substring(0, indexofValueP)));
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Remove(0, indexofValueP)));
                        goto ResultFunct;
                    }
                    else if (indexofValueBr > 0 && indexofValuePBreak == -1)
                    {
                        int[] rangeIndex = { indexofValueP, indexofValueBr };
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Substring(0, rangeIndex.Min())));
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Remove(0, rangeIndex.Min())));
                        goto ResultFunct;
                    }
                    else
                    {
                        int[] rangeIndex = { indexofValueP, indexofValuePBreak };
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Substring(0, rangeIndex.Min())));
                        ListResult.Add(HttpUtility.HtmlDecode(ContentEncode.Remove(0, rangeIndex.Min())));
                        goto ResultFunct;
                    }
                }
            }
        ResultFunct:
            return ListResult;
        }
    }
}
